<?php

namespace App\Admin\Forms\Customer;

use App\Enums\Gender;
use App\Enums\OptionType;
use App\Models\Customer;
use App\Services\OptionService;
use App\Services\SaleService;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Http\JsonResponse;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class RegisterForm extends Form implements LazyRenderable
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return JsonResponse
     */
    public function handle(array $input): JsonResponse
    {
        app(SaleService::class)->register($this->payload['id'], $input);
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $customer = Customer::query()->where('id', $this->payload['id'])->first();
        $this->text('name', trans('customer.fields.name'))->default($customer->name ?? '')->required();
        $this->text('phone', trans('customer.fields.phone'))->default($customer->phone ?? '')->required();
        $this->text('wechat', trans('customer.fields.wechat'))->default($customer->wechat ?? '');
        $this->text('id_card', trans('customer.fields.id_card'))->default($customer->id_card ?? '');
        $this->text('disease', trans('customer.fields.disease'))->default($customer->disease ?? '')->required();
        $this->select('doctor_option_id', trans('customer.fields.doctor_option_id'))->default($customer->doctor_option_id ?? 0)->options(OptionService::getOption(OptionType::DOCTOR))->required();
        $this->datetime('time_of_appointment', trans('customer.fields.time_of_appointment'))->default($customer->time_of_appointment ?? '')->required();
        $this->datetime('consultation', trans('customer.fields.consultation'))->default($customer->consultation ?? null);
        $this->textarea('symptom', trans('customer.fields.symptom'))->default($customer->symptom ?? '');
        $this->textarea('treatment_history', trans('customer.fields.treatment_history'))->default($customer->treatment_history ?? '');

        $this->number('age', trans('customer.fields.age'))->min(0)->max(100)->default($customer->age ?? 0);
        $this->select('gender', trans('customer.fields.gender'))->default($customer->gender ?? 0)->options(Gender::asSelectArray())->required();
        $this->distpicker(['province_id', 'city_id', 'district_id'])->required();
        $this->text('address', trans('customer.fields.address'))->default($customer->address ?? '');

        $province_id = $customer->province_id ?? 0;
        $city_id = $customer->city_id ?? 0;
        $district_id = $customer->district_id ?? 0;
        //回显省市区的JS代码
        $js = <<<HTML
    <script>
    $(document).ready(function() {
        setTimeout(function() {
             $("select[name='province_id']").val("{$province_id}").trigger('change');
             $("select[name='city_id']").val("{$city_id}").trigger('change');
             $("select[name='district_id']").val("{$district_id}")
        }, 100); // 延迟1秒（时间可以根据实际情况调整）

    });
</script>
HTML;
        Form::html($js);
    }

}
